package com.tanhua.api.mongo;

import com.tanhua.mongo.UserLike;
import com.tanhua.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {
@Autowired
private MongoTemplate mongoTemplate;
    @Override
    public Integer findLoveCount(Long userId) {//我喜欢的
        Query query = new Query(Criteria.where("userId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return Math.toIntExact(count);
    }

    @Override
    public Integer findFanCount(Long userId) {//喜欢我的
        Query query = new Query(Criteria.where("LikeUserId").is(userId));
        long count = mongoTemplate.count(query, UserLike.class);
        return Math.toIntExact(count);
    }

    //我喜欢的
    @Override
    public PageBeanVo findLoveList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        PageBeanVo pageBeanVo = new PageBeanVo(page, pagesize, count, userLikeList);
        return pageBeanVo;
    }

    //喜欢我的
    @Override
    public PageBeanVo findFanList(Integer page, Integer pagesize, Long userId) {
        Query query = new Query(Criteria.where("LikeUserId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        long count = mongoTemplate.count(query, UserLike.class);
        PageBeanVo pageBeanVo = new PageBeanVo(page, pagesize, count, userLikeList);
        return pageBeanVo;
    }

    @Override
    public void addFans(Long userId, Long fanId) {
        UserLike userLike = new UserLike();
        userLike.setUserId(fanId);
        userLike.setLikeUserId(userId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.insert(userLike);
    }

    @Override
    public void removeFans(Long fanId, Long userId) {
        Query query = new Query(Criteria.where("userId").is(fanId).and("likeUserId").is(userId));
        mongoTemplate.remove(query,UserLike.class);
    }

}
